package com.be.beadmin.basicData.utils;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.be.beadmin.common.exception.BeadminException;

import java.util.Objects;

public class BooleanConverter implements Converter<Integer> {

    // 导入转化
    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration){
        String value = cellData.getStringValue();
        if (value!=null) {
            if (Objects.equals(value.trim(),"是")) {
                return 1;
            }else if(Objects.equals(value.trim(),"否")){
                return 0;
            }
        }
        return null;
    }

    // 导出转化
    @Override
    public WriteCellData<String> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration){
        return new WriteCellData<>(value==1? "是":"否");
    }

    @Override
    public Class<?> supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

}
